﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MineLaunch.CommonDialog
{
    public class FolderBrowserDialog : Microsoft.Win32.CommonDialog
    {
        public string Description { get; set; }
        public Environment.SpecialFolder RootFolder { get; set; }
        public string SelectedPath { get; set; }
        public bool ShowNewFolderButton { get; set; }

        public FolderBrowserDialog()
        {
            Reset();
        }

        public override void Reset()
        {
            Description = String.Empty;
            RootFolder = Environment.SpecialFolder.Desktop;
            SelectedPath = String.Empty;
            ShowNewFolderButton = true;
        }

        private class Win32Window : System.Windows.Forms.IWin32Window
        {
            private IntPtr _handle;

            public Win32Window(IntPtr handle)
            {
                _handle = handle;
            }

            public IntPtr Handle
            {
                get
                {
                    return _handle;
                }
            }
        }

        protected override bool RunDialog(IntPtr hwndOwner)
        {
            using (var folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog())
            {
                folderBrowserDialog.Description = Description;
                folderBrowserDialog.RootFolder = RootFolder;
                folderBrowserDialog.SelectedPath = SelectedPath;
                folderBrowserDialog.ShowNewFolderButton = ShowNewFolderButton;
                if (folderBrowserDialog.ShowDialog(new Win32Window(hwndOwner)) != System.Windows.Forms.DialogResult.OK)
                {
                    return false;
                }
                SelectedPath = folderBrowserDialog.SelectedPath;
                return true;
            }
        }
    }
}
